//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：UIDisplayEvent
// *描述：UI对象事件处理脚本
// *?2021 MYJL . All rights reserved.
//* ======================================================================
using UnityEngine.UI;

namespace HDMCode.UIFrame
{
    public class UIDisplayEvent : IUIDisplayEvent
    {
        public void AddComponentsListener(UIEntity UI)
        {
            try
            {
                foreach (var component in UI.UIDisplay.components)
                {
                    foreach (var item in component.componentList)
                    {
                        string typeName = item.GetType().ToString();
                        if (item.GetType().ToString() == "UnityEngine.UI.Button")
                        {
                            var tamp = (Button)item;
                            tamp.onClick.AddListener(() =>
                            {
                                UI.UIDisplay.uiLife?.OnButtonEvent(item);
                            });
                            UIFrame.Log("UI事件监听成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.Toggle")
                        {
                            var tamp = (Toggle)item;
                            tamp.onValueChanged.AddListener((value) =>
                            {
                                UI.UIDisplay.uiLife.OnToggleEvent(item, value);
                            });
                            UIFrame.Log("UI事件监听成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.Slider")
                        {
                            var tamp = (Slider)item;
                            tamp.onValueChanged.AddListener((value) =>
                            {
                                UI.UIDisplay.uiLife.OnSliderEvent(item, value);
                            });
                            UIFrame.Log("UI事件监听成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.InputField")
                        {
                            var tamp = (InputField)item;
                            tamp.onValueChanged.AddListener((value) =>
                            {
                                UI.UIDisplay.uiLife.OnInputFieldEvent(item, value);
                            });
                            UIFrame.Log("UI事件监听成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.Scrollbar")
                        {
                            var tamp = (Scrollbar)item;
                            tamp.onValueChanged.AddListener((value) =>
                            {
                                UI.UIDisplay.uiLife.OnScrollbarEvent(item, value);
                            });
                            UIFrame.Log("UI事件监听成功！", typeName , LogSystem.UIDisplay);

                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.ScrollRect")
                        {
                            var tamp = (ScrollRect)item;
                            tamp.onValueChanged.AddListener((value) =>
                            {
                                UI.UIDisplay.uiLife.OnScrollRectEvent(item, value);
                            });
                            UIFrame.Log("UI事件监听成功！", typeName, LogSystem.UIDisplay);
                        }

                    }
                }
            }
            catch (System.Exception error)
            {
                UIFrame.LogError("UI事件监听失败！", error , LogSystem.UIDisplay);
            }
        }

        public void RemoveComponentsListener(UIDisplay UI)
        {
            try
            {
                foreach (var component in UI.components)
                {
                    foreach (var item in component.componentList)
                    {
                        string typeName = item.GetType().ToString();
                        if (item.GetType().ToString() == "UnityEngine.UI.Button")
                        {
                            var tamp = (Button)item;
                            tamp.onClick.RemoveAllListeners();
                            UIFrame.Log("UI事件移除成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.Toggle")
                        {
                            var tamp = (Toggle)item;
                            tamp.onValueChanged.RemoveAllListeners();
                            UIFrame.Log("UI事件移除成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.Slider")
                        {
                            var tamp = (Slider)item;
                            tamp.onValueChanged.RemoveAllListeners();
                            UIFrame.Log("UI事件移除成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.InputField")
                        {
                            var tamp = (InputField)item;
                            tamp.onValueChanged.RemoveAllListeners();
                            UIFrame.Log("UI事件移除成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.Scrollbar")
                        {
                            var tamp = (Scrollbar)item;
                            tamp.onValueChanged.RemoveAllListeners();
                            UIFrame.Log("UI事件移除成功！", typeName, LogSystem.UIDisplay);
                        }

                        if (item.GetType().ToString() == "UnityEngine.UI.ScrollRect")
                        {
                            var tamp = (ScrollRect)item;
                            tamp.onValueChanged.RemoveAllListeners();
                            UIFrame.Log("UI事件移除成功！", typeName, LogSystem.UIDisplay);
                        }
                    }
                }
            }
            catch (System.Exception error)
            {
                UIFrame.LogError("UI事件移除失败！", error, LogSystem.UIDisplay);
            }
            
        }
    }
}

